package bigbend_icseventgenerator;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

public class ICS_test extends BigBend_icsEventGenerator{
	
	/**
	 *	BigB Big JUnit test
	 *	Made for BigBend_icsEventGenerator
	 * 
	 *	@author Daniel Kim, Patrick Tran, Zen Ushida, & Chris Uyeda
	 */
	private static final long serialVersionUID = 4574714708932177360L;
	private BigBend_icsEventGenerator newCalender;
    private boolean saniErr;
    private boolean genErr;

	/**
	 * Sets up the variables from icsEventGenerator
	 * This would normally done with the UI in the actual program
	 * But for testing purposes, these will be set here
	 * Calls sanatize and generate output in preparation for tests
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception {
		newCalender = new BigBend_icsEventGenerator();
		newCalender.initComponents();
		newCalender.name = "This is a test";
		newCalender.classification = "PUBLIC";
		newCalender.priority = "0";
		newCalender.dtstart = "";
		newCalender.dtstart_year = "2012";
		newCalender.dtstart_month = "8";
		newCalender.dtstart_day = "7";
		newCalender.dtstart_hours = "9";
	    newCalender.dtstart_minutes = "30";
	    newCalender.dtstart_AMPM = "AM";
	    newCalender.dtend = "";
	    newCalender.dtend_year = "2012";
	    newCalender.dtend_month = "8";
	    newCalender.dtend_day = "7";
	    newCalender.dtend_hours = "10";
	    newCalender.dtend_minutes = "30";
	    newCalender.dtend_AMPM = "AM";
	    newCalender.location = "Kauai";
	    newCalender.resources = "Laptop, Cellphone";
	    newCalender.comments = "Do homework. Study for class, finish quiz.\nAnnihilate dinner";
	    newCalender.dtstart_cal = dateChooserCombo_dtstartDate.getCurrent();
	    newCalender.dtend_cal = dateChooserCombo_dtstartDate.getCurrent();
	    //Runs method to set above values, and to generate output
	    saniErr = newCalender.sanitizeInput();
	    genErr = newCalender.generateOutput();
	}

	/**
	 * Tests whether or not the user input is sanitized correctly
	 * Checks to see the important fields exist
	 * Checks to see if TZID and Times are in the proper format.
	 * Checks parsing of commas and newline.
	 */
	@Test
	public void testSanitizeInput() {
		//newCalender.sanitizeInput();

		assertNotNull(newCalender.name);
		assertNotNull(newCalender.dtstart);
		assertNotNull(newCalender.dtend);
		assertNotNull(newCalender.classification);
		assertNotNull(newCalender.priority);
		assertEquals("TZID=Pacific/Honolulu:20120807T093000", newCalender.dtstart);
		assertEquals("TZID=Pacific/Honolulu:20120807T103000", newCalender.dtend);
		assertEquals("Do homework. Study for class\\, finish quiz.\\nAnnihilate dinner", newCalender.comments);
		assertEquals("Laptop\\, Cellphone", newCalender.resources);
		
		/*
		 * If no errors were found during sanitization process, this passes
		 * May be caused by naming errors. Though, technically means it's also working.
		 */
		assertFalse(saniErr);
	}
	
	/**
	 * Tests the final output file
	 * Makes sure each line starts off properly, as per the spec
	 * Makes sure all recommended fields are there
	 * Does not check each field for proper input (should already have been checked by sanitize)
	 * @throws IOException
	 */

	@Test
	public void testGenerateOutput() throws IOException {
		//newCalender.generateOutput();
		FileReader readFile = new FileReader("myevent.ics");
		BufferedReader in = new BufferedReader(readFile);
		assertTrue(in.readLine().startsWith("BEGIN:VCALENDAR"));
		assertTrue(in.readLine().startsWith("VERSION:2.0"));
		assertTrue(in.readLine().startsWith("BEGIN:VEVENT"));
		assertTrue(in.readLine().startsWith("CLASS:"));
		assertTrue(in.readLine().startsWith("PRIORITY:"));
		assertTrue(in.readLine().startsWith("DTSTART;TZID"));
		assertTrue(in.readLine().startsWith("DTEND;TZID"));
		assertTrue(in.readLine().startsWith("SUMMARY:"));
		if(newCalender.location != null)
			assertTrue(in.readLine().startsWith("LOCATION:"));
		if(newCalender.resources != null)
			assertTrue(in.readLine().startsWith("RESOURCES:"));
		if(newCalender.comments != null){
			assertTrue(in.readLine().startsWith("COMMENTS:"));
			assertTrue(in.readLine().startsWith("DESCRIPTION:"));
		}
		assertTrue(in.readLine().startsWith("END:VEVENT"));
		assertTrue(in.readLine().startsWith("END:VCALENDAR"));
		
		/*
		 * If no errors were found in main program during creation process of file, this passes
		 * Though, technically means it's also working
		 * */
		assertFalse(genErr);
	}
}
